var id= "0";
var obj = "0";
var img_url = {};
var img_name = {};
var img_urlId = {}
function preview(file) {
    let ret = ($(".li").length);
    if (ret == 4) {
        $.toast("附件最多上传4个！", "cancel");
        return false
    }
    var location=$("input[name='uploaderInput']").val();
    var point = location.lastIndexOf(".");
    var type = location.substr(point);
    // $(".li img").each(function(){
    //     url_img.push($(this).attr('src'))
    // });
    var prevDiv = $(file).parent().find('.img-cont');
    if (file.files && file.files[0]) {
        var reader = new FileReader();
        reader.onload = function(evt) {
            var fileObj = document.getElementById("FileUpload").files[0]; // js 获取文件对象
            var formFile = new FormData();
            formFile.append("key", "Gn1xVdBiTClSSHKZifg0pTQSU5XGagWO");
            formFile.append("file", fileObj); //加入文件对象
            var data = formFile;
            $.ajax({
                url: "http://ydnshop.hepulanerp.com/sucai_t/public/index.php/post/post/uploadFile",
                data: data,
                type: "Post",
                dataType: "json",
                cache: false,//上传文件无需缓存
                processData: false,//用于对data参数进行序列化处理 这里必须false
                contentType: false, //必须
                success: function (result) {
                    img_url[obj] = result.info.url;
                    img_name[obj] = result.info.name
                    img_urlId['img' + obj] = result.info.url
                    obj ++;
                },
                error: function () {}
            });
            if(type==".jpg"||type==".png"||type==".jpeg"){
                prevDiv.append("<div id='img"+ id + "' class='li'><img src='"+ evt.target.result +"' class='img' /><a id='"+id+"' class='diySuccess hide delete-btn'></a></div>");
                ImagesZoom.init({
                    "elem": ".list"
                });
            }
            if(type==".docx"||type==".doc"||type==".pdf"){
                prevDiv.append("<div id='img"+ id + "' class='li txt_diy_bg'><img src='' class='img' style='opacity: 0'/><a id='"+id+"' class='diySuccess hide delete-btn'></a></div>");
            }
            id ++;
        }
        reader.readAsDataURL(file.files[0]);
    } else {//IE
        prevDiv.append('<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>');
    }
    // $(file).parent().find('.delete').show();
}
function ulrHtml(num) {
    let name = $('.name').val()
    let sex = $("input[type='radio'][name='radio1']:checked").val()
    let phone = $('.phone').val()
    sessionStorage.setItem("name", name);
    sessionStorage.setItem("sex", sex);
    sessionStorage.setItem("phone", phone);
    window.location.href = "establish.html";
}
function ajax(url, val, cb) {
    let key = '195a9b99ea7a0d259151b5c412bb631b';
    let val1 = val
    $.ajax({
        type: "POST",
        url: url,
        data: {api_key: key, typeTrench: val1},
        dataType: "json",
        success: function (response) {
            if (typeof cb == "function") {
                cb(response)
            }
        }
    });
}
window.onload = function () {
    // 展开与收起
    var gs = 0;
    $('.gs').click(function (e) {
        e.stopPropagation()
        if($(this).get(0).checked){
            $(this).parents('.weui_cell_list').next('.inputBox').slideDown("slow");
            gs = 1
        } else {
            $(this).parents('.weui_cell_list').next('.inputBox').slideUp("slow")
            gs = 0
        }
    })
    var sx = 0;
    $('.sx').click(function (e) {
        e.stopPropagation()
        if($(this).get(0).checked){
            $(this).parents('.weui_cell_list').next('.inputBox').slideDown("slow");
            sx = 1
        } else {
            $(this).parents('.weui_cell_list').next('.inputBox').slideUp("slow")
            sx = 0
        }
    })
    var buy = 0;
    $('.buy').click(function (e) {
        e.stopPropagation()
        if($(this).get(0).checked){
            $(this).parents('.weui_cell_list').next('.inputBox').slideDown("slow");
            buy = 1
        } else {
            $(this).parents('.weui_cell_list').next('.inputBox').slideUp("slow")
            buy = 0
        }
    })
    var shihe = 0;
    $('.shihe').click(function (e) {
        e.stopPropagation()
        if($(this).get(0).checked){
            $(this).parents('.weui_cell_list').next('.inputBox').slideDown("slow");
            shihe = 1
        } else {
            $(this).parents('.weui_cell_list').next('.inputBox').slideUp("slow")
            shihe = 0
        }
    })
    // 获取应聘职位以及应聘类型
    var positionList;
    var typeTrench = [];
    var position1 = [];
    var position2 = [];
    var typeTrench2 = [];
    var typeTval = [];
    positionList = ajax("http://hplqytest.suoluomei.cn/index.php?s=/Hr/Index/getPosition_Channel_typeJob", '', function (res) {
        let data = res;
        position1 = data.data.position
        typeTrench = data.data.channel_type
        for (let index in position1) {
            position2[index] = position1[index].name
        }
        for (let index in typeTrench) {
            typeTrench2[index] = typeTrench[index].name
            typeTval[index] = typeTrench[index]
        }
    })
    // 应聘类型
    var trenchList;
    var trench = [];
    var tempArr = [];
    $("#typeTrench").picker({
        title: "-- 请选择 --",
        cols: [
            {
                textAlign: 'center',
                values: typeTrench2
            }
        ],
        onClose: function (picker) {
            tempArr.length = 0
            let comparison = []
            let comparisonvalue = []
            $('#typeTrench').val(picker.cols[0].value)
            let value = $('#typeTrench').val()
            for (let index in typeTval) {
                comparison[index] = typeTval[index].name
                comparisonvalue[index] = typeTval[index].value
                if (comparison[index] == value) {
                    // 拿到应聘类型value获取应聘渠道
                    trenchList = ajax("http://hplqytest.suoluomei.cn/index.php?s=/Hr/Index/getChannelJob", comparisonvalue[index], function (res) {
                        $("#trench").val('')
                        let data = res.data.channel;
                        trench = data
                        for (let index in data) {
                            tempArr[index] = data[index].name
                        }
                    })
                }
            }
        }
    });
    // 应聘渠道
    $("#trench").picker({
        title: "-- 请选择 --",
        cols: [
            {
                textAlign: 'center',
                values: tempArr
            }
        ],
        onOpen: function (picker) {
            picker.setValue(tempArr);
            picker.updateValue()
        },
        onClose: function (picker) {
            picker.updateValue()
        }
    });
    // 应聘职位
    $("#position").picker({
        title: "-- 请选择 --",
        cols: [
            {
                textAlign: 'center',
                values: position2
            }
        ]
    });
    // 验证正则表达式
    $(".verify").blur(function () {
        var id = $(this).attr("id");
        var pattern = $("#" + id).val();
        // 手机
        if (id === 'phone' || id === 'phone1') {
            if (pattern === '' || pattern && /^1[3|4|5|8]\d{9}$/.test(pattern)) {
            } else {
                $.toptip('手机号码输入错误', 'error');
                return false
            }
        }
        // 身份证
        if (id === 'pattern') {
            // var pattern = $("#" + id).val();
            if (pattern === '' || pattern && /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(pattern)) {
            } else {
                $.toptip('身份证号码输入错误', 'error');
                return false
            }
        }
        // 邮箱
        if (id === 'email') {
            if (pattern === '' || pattern && /^\w+@\w+(.[a-zA-Z]{2,3}){1,2}$/.test(pattern)) {
            } else {
                $.toptip('邮箱输入错误', 'error');
                return false
            }
        }
    });
    // 婚姻0
    $("#marriage").picker({
        title: "-- 请选择 --",
        cols: [
            {
                textAlign: 'center',
                values: ["未婚", "已婚已育", "已婚未育", "其他"]
            }
        ]
    });
    // 日历
    $(document).on('touchstart', '.my-input', function () {
        var id = $(this).attr("id");
        $("#" + id).datetimePicker({
            title: '-- 请选择日期 --',
            min: "1990-12-12",
            max: "2070-12-12 12:12",
            times: function () {
                return [];
            },
            onChange: function (picker, values, displayValues) {
            }
        });
        $(".outfocus").blur();
    });
    // 离焦事件
    $(document).on('touchstart', '.choice', function () {
        $(".outfocus").blur();
    });
    //删除图片
    $(document).on('click', '.hide', function (){
        let id = $(this).attr("id");
        delete img_url[id];
        delete img_name[id];
        $(this).parent().remove();
    })
    // 遍历上传文档
    $(document).on('click', '.txt_diy_bg', function (){
        let id = $(this).attr("id");
        for (let item in img_urlId) {
            if (item == id) {
                location.href = img_urlId[item]
            }
        }
    })
    // 提交
    $(document).on('click', '#btnSub', function () {
        let yTime = $('.yTime').val()
        for (let index in position1) {
            if ($('.position').val() == position1[index].name) {
                var position = position1[index].value
            }
        }
        for (let index in typeTval) {
            if ($('.typeTrench').val() == typeTval[index].name) {
                var typeTrench = typeTval[index].value
            }
        }
        for (let index in trench) {
            if ($('.channel').val() == trench[index].name) {
                var channel = trench[index].value
            }
        }
        let name = $('.name').val()
        let sex = $("input[type='radio'][name='radio1']:checked").val()
        let phone = $('.phone').val()
        let dz = $('.dz').val()
        let ssz = $('.ssz').val()
        let hy = $('.hy').val()
        let gsName = $('.gsName').val()
        let gsGuan = $('.gsGuan').val()
        let sxName = $('.sxName').val()
        let sxGuan = $('.sxGuan').val()
        let buyName = $('.buyName').val()
        let noShihe = $('.noShihe').val()
        let url_img = []
        for (let item in img_url) {
            url_img.push(img_url[item])
        }
        let url_name = []
        for (let item in img_name) {
            url_name.push(img_name[item])
        }
        if (yTime == '' || yTime == null || yTime == undefined) {
            $.alert('应聘日期不能为空!', '温馨提示');
            return false;
        }
        if (position == '' || position == null || position == undefined) {
            $.alert('应聘职位不能为空!', '温馨提示');
            return false;
        }
        if (typeTrench == '' || typeTrench == null || typeTrench == undefined) {
            $.alert('渠道类型不能为空!', '温馨提示');
            return false;
        }
        if (channel == '' || channel == null || channel == undefined) {
            $.alert('应聘渠道不能为空!', '温馨提示');
            return false;
        }
        if (name == '' || name == null || name == undefined) {
            $.alert('姓名不能为空!', '温馨提示');
            return false;
        }
        if (phone == '' || phone == null || phone == undefined) {
            $.alert('手机不能为空!', '温馨提示');
            return false;
        }
        let ph = $("#phone").val();
        // 手机
        if (ph && /^1[3|4|5|8]\d{9}$/.test(ph)) {
        } else {
            $.toptip('手机号码输入错误', 'error');
            return false
        }
        let pattern = $("#pattern").val();
        // 身份证
        if (pattern && /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(pattern)) {
        } else {
            $.toptip('身份证号码输入错误', 'error');
            return false
        }
        $.modal({
            title: "温馨提示",
            text: "请再次检查信息，确认资料属实、无误！",
            buttons: [
                {
                    text: "确定",
                    onClick: function () {
                        let key = '195a9b99ea7a0d259151b5c412bb631b';
                        $.ajax({
                            type: "post",
                            url: "http://hplqytest.suoluomei.cn/index.php?s=/Hr/Index/applySave",
                            data: {
                                api_key: key,
                                yTime: yTime,
                                job_id: position,
                                typeTrench: typeTrench,
                                channel_id: channel,
                                name: name,
                                sex: sex,
                                mobile: phone,
                                address: dz,
                                idcard: ssz,
                                marriage: hy,
                                url: url_img,
                                urlname: url_name,
                                is_buy_product: buy,
                                is_company_relatives: gs,
                                is_compete_relatives: sx,
                                is_not_arrange_work: shihe,
                                company_relatives_name: gsName,
                                company_relatives_relation: gsGuan,
                                compete_relatives_name: sxName,
                                compete_relatives_relation: sxGuan,
                                hf_teacher_name: buyName,
                                not_arrange_work: noShihe,
                            },
                            success: function (result) {
                                $.toast("提交成功");
                                $("input[type='text']").val('')
                                $("input[type='number']").val('')
                                $(".li").remove();
                            },
                            error: function () {
                                $.toast("提交失败");
                            }
                        });
                    }
                },
                {
                    text: "取消",
                    className: "default",
                    onClick: function () {
                    }
                },
            ]
        });
        return false;
    });

};

// $(window).scroll(function () {
//     if( $(this).scrollTop() > 200 ){
//         $("#fixBox").show().animate({"top":"100px"}, 1000);
//     }else{
//         //小于200的时候top回到原来的地方
//         //$("#fixBox").hide().css("top","300px");
//         $("#fixBox").stop(true,true).hide().css("top","300px");
//     }
// })

